package cn.imvc.app.models;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.Resource;
import cn.imvc.app.entities.Role;
import cn.imvc.app.repositories.ResourceRepository;
import cn.imvc.app.repositories.RoleRepository;
import cn.imvc.core.HttpContext;
@Service
public class RoleResourceModelImpl implements RoleResourceModel {
	@Autowired
	private RoleRepository roleRepository;
	@Autowired
	private ResourceRepository resourceRepository;
	
	public String getRoles() {
		List<Role> roleList=roleRepository.findAll(new Sort(Direction.DESC,"order"));
		StringBuffer roles=new StringBuffer();
		for (Role role : roleList) {
			roles.append("<li><a href='"+HttpContext.getRootUrl()+"/system/role-resource/"+role.getId()+"' target='ajax' rel='resourceTree'>"+role.getName()+"</a></li>");
		}
		return roles.toString();
	}
	public String getResources(int roleId) {
		List<Resource> resources = resourceRepository.findAll();
		StringBuffer str = new StringBuffer();
		String checked="";
		Role role=roleRepository.findOne(roleId);
		for (Resource resource : resources) {
			if(resource.getRole().contains(role)){
				checked="checked='true'";
			}else{
				checked="";
			}
			str.append("<li><a tname='resourceid' tvalue='"+resource.getId()+"' "+checked+" >"+resource.getName()+"</a></li>");
		}
		return str.toString();
	}
	
	public void saveRoleResource(int id,Set<Resource> resources){
		Role role=roleRepository.findOne(id);
		for (Object object : role.getResource()) {
			Resource resource=(Resource)object;
			resource.getRole().remove(role);
			
		}
		role.getResource().clear();
		roleRepository.saveAndFlush(role);
		
		role.getResource().addAll(resources);
		for (Object object : role.getResource()) {
			Resource resource=(Resource)object;
			resource.getRole().add(role);
			
		}
		roleRepository.saveAndFlush(role);
	}
	public String getResources() {
		List<Resource> resourceList=resourceRepository.findAll();
		StringBuffer resources=new StringBuffer();
		for (Resource resource : resourceList) {
			resources.append("<li><a href='"+HttpContext.getRootUrl()+"/system/resource-role/"+resource.getId()+"' target='ajax' rel='roleTree'>"+resource.getName()+"</a></li>");
		}
		return resources.toString();
	}
	public String getRoleTree(int resourceId) {
		List<Role> roles = roleRepository.findAll();
		StringBuffer str = new StringBuffer();
		String checked="";
		Resource resource=resourceRepository.findOne(resourceId);
		for (Role role : roles) {
			if(role.getResource().contains(resource)){
				checked="checked='true'";
			}else{
				checked="";
			}
			str.append("<li><a tname='roleid' tvalue='"+role.getId()+"' "+checked+" >"+role.getName()+"</a></li>");
		}
		return str.toString();
	}
	public void saveResourceRole(int id, Set<Role> roles) {
		
		Resource resource=resourceRepository.findOne(id);
		for (Object object : resource.getRole()) {
			Role role=(Role)object;
			role.getResource().remove(resource);
		}
		resource.getRole().clear();
		resourceRepository.saveAndFlush(resource);
		
		resource.getRole().addAll(roles);
		for (Object object : resource.getRole()) {
			Role role=(Role)object;
			role.getResource().add(resource);
		}
		resourceRepository.saveAndFlush(resource);
		
	}
	

}
